package com.aigc.sclassroom.service.impl;

import com.aigc.sclassroom.mapper.HomeworkMapper;
import com.aigc.sclassroom.pojo.*;
import com.aigc.sclassroom.service.HomeworkService;
import com.aigc.sclassroom.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class HomeworkServiceImpl implements HomeworkService {
    @Autowired
    private HomeworkMapper homeworkMapper;
    @Override
    public List<Homework> selectAll(Long courseManageId) {
        return homeworkMapper.selectHomeworkAll(courseManageId);
    }

    @Override
    public String[] selectQuestion(Long homeworkId) {
        return homeworkMapper.selectQuestionByHomeworkId(homeworkId);
    }

    @Override
    public Long[] selectId(Long homeworkId) {
        return homeworkMapper.selectQuestionIdByHomeworkId(homeworkId);
    }

    @Override
    public String[] selectAnswer(Long homeworkId) {
        return homeworkMapper.selectAnswerByHomeworkId(homeworkId);
    }

    @Override
    public int[] selectScoreSet(Long homeworkId) {
        return homeworkMapper.selectScoreSetByHomeworkId(homeworkId);
    }

    @Override
    public void addHomework(Homework homework) {
        homeworkMapper.addHomework(homework);
    }

    @Override
    public void addQuestion(String[] question, String[] answer,int[] scoreSet,Long homeworkId) {
        for (int i = 0; i < question.length; i++) {
            homeworkMapper.addQuestion(question[i],answer[i],scoreSet[i],homeworkId);
        }
    }

    @Override
    public List<HomeworkResult> selectResult(Long homeworkId) {
        return homeworkMapper.selectResult(homeworkId);
    }

    @Override
    public List<StuAnswer> selectStuAnswer(Long homeworkId,Long userId) {
        return homeworkMapper.selectStuAnswer(homeworkId,userId);
    }

    @Override
    public void updateScore(StuAnswer stuAnswer) {
        homeworkMapper.updateScore(stuAnswer);
    }

    @Override
    public int selectSum(Long courseManageId) {
        return homeworkMapper.selectSum(courseManageId);
    }

    @Override
    public int selectSubmitCount(Long homeworkId) {
        return homeworkMapper.selectSubmitCount(homeworkId);
    }

    @Override
    public void submitAnswer(List<Answer> answers) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        Long userId =(id.longValue());
        for (Answer answer: answers){
            answer.setUserId(userId);
            homeworkMapper.insertAnswer(answer);
            Long studentAnswerId = homeworkMapper.selectStudentAnswerId(answer);
            homeworkMapper.insertGrade(studentAnswerId);
        }

    }

    @Override
    public List<Long> selectStuAnswerId(Long userId, Long homeworkId) {
        return homeworkMapper.selectId(userId,homeworkId);
    }

    @Override
    public Integer selectScore(Long studentAnswerId) {
        return homeworkMapper.selectScore(studentAnswerId);
    }

    @Override
    public AverageCount selectAverage(Long homeworkId) {
        return homeworkMapper.selectAverage(homeworkId);
    }

    @Override
    public void updateStatusToExpired() {
            LocalDateTime now = LocalDateTime.now();
            homeworkMapper.updateExpiredHomeworkStatus(now, "已截止");
    }

    @Override
    public Integer selectTotalScore(Long homeworkId, Long userId) {
        return homeworkMapper.selectTotalScore(homeworkId,userId);
    }
}
